package com.atguigu.gmall.cart.controller;

import com.atguigu.gmall.activity.client.ActivityFeignClient;
import com.atguigu.gmall.cart.service.CartService;
import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.common.util.AuthContextHolder;
import com.atguigu.gmall.model.cart.CarInfoVo;
import com.atguigu.gmall.model.cart.CartInfo;
import org.apache.http.auth.AUTH;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author ：Shine
 * @description：
 * @date ：2021/2/26 10:36
 */
@RestController
@RequestMapping("/api/cart")
public class CartApiController {

    @Autowired
    private CartService cartService;

    @Autowired
    private ActivityFeignClient activityFeignClient;

    @PostMapping("addToCart/{skuId}/{skuNum}")
    public Result addToCart(@PathVariable Long skuId,
                            @PathVariable Integer skuNum,
                            HttpServletRequest request) {
        //在header中存储了userId，所以通过request去获取，这边已经封装好了一个方法
        String userId = AuthContextHolder.getUserId(request);

        //如果要实现必须登录才可添加购物车的话，只需要咋网关的配置文件中添加一个addCart.html
        //目前是未登录也可以添加，查看，删除，修改购物车

        if (StringUtils.isEmpty(userId)) {
            //如果未登录的话，request中获取的userId为空，此时存一个临时id
            userId = AuthContextHolder.getUserTempId(request);
        }
        cartService.addToCart(skuId, userId, skuNum);
        return Result.ok();

    }

    @GetMapping("cartList")
    public Result cartList(HttpServletRequest request) {
        // 获取用户Id
        String userId = AuthContextHolder.getUserId(request);
        // 获取临时用户Id
        String userTempId = AuthContextHolder.getUserTempId(request);
        List<CartInfo> cartInfoList = cartService.getCartInfoList(userId, userTempId);

        Long curentUserId = StringUtils.isEmpty(userId) ? null : Long.parseLong(userId);
        List<CarInfoVo> carInfoVoList = activityFeignClient.findCartActivityAndCoupon(cartInfoList, curentUserId);
        return Result.ok(carInfoVoList);
    }


    @GetMapping("checkCart/{skuId}/{isChecked}")
    public Result checkCart(@PathVariable Long skuId,
                            @PathVariable Integer isChecked,
                            HttpServletRequest request) {
        //从request对象中获取userId
        String userId = AuthContextHolder.getUserId(request);

        if (StringUtils.isEmpty(userId)) {
            userId = AuthContextHolder.getUserTempId(request);
        }

        cartService.checkCart(userId, skuId, isChecked);
        return Result.ok();
    }

    @DeleteMapping("deleteCart/{skuId}")
    public Result deleteCart(@PathVariable Long skuId, HttpServletRequest request) {
        String userId = AuthContextHolder.getUserId(request);

        if (StringUtils.isEmpty(userId)) {
            userId = AuthContextHolder.getUserTempId(request);
        }

        cartService.deleteCart(userId, skuId);
        return Result.ok();
    }


    @GetMapping("getCartCheckedList/{userId}")
    public List<CartInfo> getCartCheckedList(@PathVariable String userId) {
        return cartService.getCartCheckedList(userId);
    }

    //    根据用户id查询商品的最新价格
    @GetMapping("loadCartCache/{userId}")
    public Result loadCartCache(@PathVariable String userId) {
        cartService.loadCartCache(userId);
        return Result.ok();
    }

}
